package com.ctsi.question.service;

import com.ctsi.commons.util.UtilValidate;
import com.ctsi.commons.util.id.UUIDGenerator;
import com.ctsi.commons.util.response.Page;
import com.ctsi.framework.mybatis.support.MybatisGenericDao;
import com.ctsi.question.dto.ProjectStageInfo;
import com.ctsi.question.dto.ReportProjectDTO;
import com.ctsi.question.dto.WeekReportDetailDTO;
import com.ctsi.question.entity.WeekReportDetailEntity;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;


/**
 * WeekReportDetailEntity Service 类
 * @author   zhangjw
 * @description  WeekReportDetailEntity
 * @created	 Wed Aug 28 16:56:50 CST 2019
 */

@Service
public class WeekReportDetailEntityService {
	private static final Logger logger=LoggerFactory.getLogger(WeekReportDetailEntityService.class);
	@Autowired
	private MybatisGenericDao dao;

	/**
	 * 根据当前用户查询所属项目
	 * @param userId
	 * @return
	 */
	public List<ReportProjectDTO> findReportProjects(String userId){
		Map<String,Object> params = new HashMap<>();
		params.put("nowTime",System.currentTimeMillis());
		params.put("userId",userId);
		return (List<ReportProjectDTO>) dao.queryForList(WeekReportDetailEntity.class.getName()+".findNeedReportItems",params);
	}

	/**
	 * 查询项目计划进度信息，用于判断是否延期
	 * @param userId
	 * @param projectCode
	 * @return
	 */
	public ProjectStageInfo findProjectStageInfo(String userId,String projectCode){
		Map<String,Object> params = new HashMap<>();
		params.put("userId",userId);
		params.put("projectCode",projectCode);
		params.put("nowTime", System.currentTimeMillis());
		return (ProjectStageInfo) dao.queryForObject(WeekReportDetailEntity.class.getName()+".isDelay",params);
	}


	/**
	 * 通过周报ID，查询周报详情
	 * @param reportId
	 * @return
	 */
	public List<WeekReportDetailDTO> details(String reportId, String userId){
		Map<String,Object> params = new HashMap<>();
		params.put("reportId",reportId);
		params.put("userId",userId);
		return (List<WeekReportDetailDTO>) dao.queryForList(WeekReportDetailEntity.class.getName()+".findReportDetails",params);
	}

	/**
	 * 通过详情ID，查询详情
	 * @param detailId
	 * @param userId
	 * @return
	 */
	public WeekReportDetailDTO detail(String detailId, String userId){
		Map<String,Object> params = new HashMap<>();
		params.put("detailId",detailId);
		params.put("userId",userId);
		return (WeekReportDetailDTO) dao.queryForObject(WeekReportDetailEntity.class.getName()+".findReportDetails",params);
	}

	/**
	 * 添加
	 */
	public void insertWeekReportDetailEntity(WeekReportDetailEntity weekReportDetailEntity){
		if(UtilValidate.isEmpty(weekReportDetailEntity.getId()))
			weekReportDetailEntity.setId(UUIDGenerator.getId());
		logger.debug("insert {}",weekReportDetailEntity);
		dao.insert(weekReportDetailEntity);
	}
	/**
	 * 动态更新
	 */
	public int updateWeekReportDetailEntityByPKSelective(WeekReportDetailEntity weekReportDetailEntity){
		logger.debug("updateByPrimaryKeySelective {}",weekReportDetailEntity);
		return dao.update(WeekReportDetailEntity.class.getName()+".updateByPrimaryKeySelective", weekReportDetailEntity);
	}
	/**
	 * 更新
	 */
	public int updateWeekReportDetailEntity(WeekReportDetailEntity weekReportDetailEntity){
		logger.debug("update {}",weekReportDetailEntity);
		return dao.update(weekReportDetailEntity);
	}


	public WeekReportDetailEntity editWeekReportDetailEntityById(String id){
		if(UtilValidate.isEmpty(id))
			return null;
		logger.debug("editById {}",id);
		return (WeekReportDetailEntity)dao.queryForObject(WeekReportDetailEntity.class.getName()+".edit", id);
	}
	/**
	 * 根据ID查找 WeekReportDetailEntity对象
	 */
	public WeekReportDetailEntity findWeekReportDetailEntityById(String id){
		if(UtilValidate.isEmpty(id))
			return null;
		logger.debug("findById {}",id);
		return dao.get(WeekReportDetailEntity.class, id);
	}
	/**
	 * 删除WeekReportDetailEntity对象
	 */
	public int removeWeekReportDetailEntity(String id){
		if(UtilValidate.isNotEmpty(id))
			return dao.delete(WeekReportDetailEntity.class.getName()+".delete", id);
			//return dao.remove(id);
		return 0;
	}

	/**
	 * 详细页查询
	 */
	public WeekReportDetailEntity findByDetail(String id){
		return (WeekReportDetailEntity)dao.queryForObject(WeekReportDetailEntity.class.getName()+".findByDetail",id);
	}
	/**
	 * 多条件查询结果
	 */
	public Page<WeekReportDetailEntity> findByMulitCondition(Map<String,Object> param , int start , int pageSize){
		logger.debug("findByMulitCondition {}",param);
		if(pageSize<0||pageSize>2000)
			pageSize=Page.DEFAULT_PAGE_SIZE;
		if(start!=-1)
			return dao.queryPage(WeekReportDetailEntity.class.getName()+".selectMulitCondition", param, start, pageSize);
		else{
			throw new RuntimeException("参数不正确 !!");
		}
	}
	/**
	 * 多条件查询结果不分页
	 */
	public List<WeekReportDetailEntity> find(Map<String,Object> map){
		logger.debug("find {}",map);
		return (List<WeekReportDetailEntity>)dao.queryForList(WeekReportDetailEntity.class.getName()+".selectByMap",map);
	}

}
